package com.icoopoo.log.file;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.icoopoo.log.file.sub.FileService;
import com.icoopoo.log.file.sub2.FileService2;

import javax.servlet.http.HttpServletRequest;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * @Author : leih
 * @Date : 2020/8/31 13:35
 * @Version : 1.0
 * @Description : TODO
 * @ClassName : ConsoleController
 */

@RestController
@RequestMapping("/a")
@Validated
public class FileController {

    private final static Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService consoleService;

    @Autowired
    private FileService2 consoleService2;

    @PostMapping(value = "log")
    public String listAlarm(Integer numLine) {

        if(numLine == null ){
            numLine = 1;
        }
        for (int i = 0 ;i < numLine; i++){
            logger.error("FileController    在classpath下寻找是否有logback-test.xml，在classpath下寻找是否有logback.xml");
            logger.warn("FileController     在classpath下寻找是否有logback-test.xml，在classpath下寻找是否有logback.xml");
            logger.info("FileController     在classpath下寻找是否有logback-test.xml，在classpath下寻找是否有logback.xml");
            logger.debug("FileController    在classpath下寻找是否有logback-test.xml，在classpath下寻找是否有logback.xml");
            logger.trace("FileController    在classpath下寻找是否有logback-test.xml，在classpath下寻找是否有logback.xml");
        }
        consoleService.logTest(numLine);
        consoleService2.logTest(numLine);

        return "success";
    }

    @PostMapping(value = "b")
    public void addVisitor(@RequestBody String str) {
        logger.info("Certification verification machine add visitor info:{}",str);
        Gson gson = new Gson();
        Verification verification = gson.fromJson(str, Verification.class);
        logger.info(" person check ID Card info : {}",verification);
    }

    @PostMapping(value = "getIp")
    public void getIp(@RequestBody String str, HttpServletRequest request) throws SocketException, UnknownHostException {
        logger.info("Certification verification machine add visitor info:{}",str);
        String ip = IpUtil.getIpAddr(request);
        String mac = IpUtil.getLocalMac(ip);
        logger.debug("ip:{}, mac:{}",ip,mac);
    }
}
